
package Controller;
import View.View;
import mvc.Model.Employee;
import mvc.Model.Model;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

import mvc.Model.Department;
import mvc.Model.Employee;
import mvc.Model.Model;
import java.io.*;
import java.util.ArrayList;
import java.*;
import java.util.Scanner;
import Model.Model;

import java.io.*;
import java.util.ArrayList;


public class Controller {
    Model model;
    View view;

    public Controller(Model model, View view)
    {
       this.model = model;
       this.view = view;
    }

    public void enterInApplication()
    {    
        String name = null;
        int select = -1;
        Scanner scan = new Scanner(System.in);
        File fileofEmployee = new File("Save.cx");
        File fileofDepartments = new File ("Save2.cx");
        view.menu();
        view.setSelect();
        select = scan.nextInt();

        int select4 = -1;

        while(select != 0)
        {
            switch (select)
            {
                case 1:
                {   
                   int select3 = -1;
                   int select2 = -1;
                   scan = new Scanner(System.in);
                   view.setNameOfDepartment();

                   name = scan.nextLine();
                    
                   while (name == "")
                   {
                       scan = new Scanner(System.in);
                       view.errorEnter();
                       view.setNameOfDepartment();
                       name = scan.nextLine();
                   }

                    view.menuForChiefOfDepartment();

                    select2 = scan.nextInt();
                    switch(select2)
                    {
                        case 1:
                        {
                            Department dep = new Department(name, null) ;
                            model.addDepartment(dep);
                            view.succesfulEnter();
                        } break;

                        case 2:
                        {
                            if(model.getCount()!=0)
                            {
                            scan = new Scanner(System.in);
                            model.printEmployee();
                            view.setIndexOfEmployee();
                            select3 = scan.nextInt();
                            
                            while(select3 < 0 || select3 > model.getCount() - 1)
                            {
                                view.errorEnter();
                                view.setIndexOfEmployee();
                                select3 = scan.nextInt();
                            }

                            Department dep = new Department(name, model.getEmployeeById(select3)) ;
                            model.addDepartment(dep);
                            view.succesfulEnter();
                            }

                            else {view.listEmpty();}
                        } break;

                        case 3:
                        {
                            scan = new Scanner(System.in);
                            view.setName();
                            String nameOfEmp = scan.nextLine();
                            while (nameOfEmp == "")
                            {   
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setName();
                                nameOfEmp = scan.nextLine();
                            }

                            view.setMiddleName();
                            String midNameOfEmp = scan.nextLine();
                            while (midNameOfEmp=="")
                            {   
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setMiddleName();
                                midNameOfEmp = scan.nextLine();
                            }

                            view.setLastName();
                            String lastnameOfEmp = scan.nextLine();
                            while (lastnameOfEmp == "")
                            {   
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setLastName();
                                lastnameOfEmp = scan.nextLine();
                            }

                            view.setPay();
                            String payOfEmp = scan.nextLine();
                            while (payOfEmp== "")
                            {   
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setPay();
                                payOfEmp = scan.nextLine();
                            }

                            view.setPhone();
                            String phoneOfEmp = scan.nextLine();
                            while (phoneOfEmp == "")
                            {   
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setPhone();
                                phoneOfEmp = scan.nextLine();
                            }


                            Employee employee = new Employee(nameOfEmp, midNameOfEmp, lastnameOfEmp, null, phoneOfEmp, payOfEmp);

                            Department department = new Department(name, employee);

                            employee.setDepartment(department);
                            model.addDepartment(department);
                            model.addEmployee(employee);    
                        } break;

                        default: view.errorEnter();
                    }
                }
                break;

                case 2:
                {
                    scan = new Scanner(System.in);
                    view.setName();
                    String nameOfEmp = scan.nextLine();
                    while (nameOfEmp == "")
                    {    scan = new Scanner(System.in);
                        view.errorEnter();
                        view.setName();
                        nameOfEmp = scan.nextLine();
                    }

                    view.setMiddleName();
                    String midNameOfEmp = scan.nextLine();
                    while (midNameOfEmp=="")
                    {    
                        scan = new Scanner(System.in);
                        view.errorEnter();
                        view.setMiddleName();
                        midNameOfEmp = scan.nextLine();
                    }

                    view.setLastName();
                    String lastnameOfEmp = scan.nextLine();
                    while (lastnameOfEmp=="")
                    {   
                        scan = new Scanner(System.in);
                        view.errorEnter();
                        view.setLastName();
                        lastnameOfEmp = scan.nextLine();
                    }

                    view.setPay();
                    String payOfEmp = scan.nextLine();
                    while (payOfEmp=="")
                    {    
                        scan = new Scanner(System.in);
                        view.errorEnter();
                        view.setPay();
                        payOfEmp = scan.nextLine();
                    }

                    view.setPhone();
                    String phoneOfEmp = scan.nextLine();
                    while (phoneOfEmp=="")
                    {    scan = new Scanner(System.in);
                        view.errorEnter();
                        view.setPhone();
                        phoneOfEmp = scan.nextLine();
                    }

                    view.setDepartment();
                    view.setTypeOfDepartment();
                    scan = new Scanner(System.in);
                    select4 = scan.nextInt();

                    switch (select4)
                    {
                        case 1:
                        {
                            view.setNameOfDepartment();
                            scan = new Scanner(System.in);
                            String name3 = scan.nextLine();
                            while (name3.equals(""))
                            {
                                scan = new Scanner(System.in);
                                view.errorEnter();
                                view.setNameOfDepartment();
                                name3 = scan.nextLine();
                            }

                            view.menuForChiefOfDepartmentTwo();

                            int select5 = scan.nextInt();
                            switch(select5)
                            {
                                case 1:
                                {
                                    Department dep = new Department(name3, null) ;

                                    Employee employee = new Employee(nameOfEmp,midNameOfEmp,lastnameOfEmp, dep, phoneOfEmp,payOfEmp);
                                    model.addEmployee(employee);
                                    model.addDepartment(dep);
                                    view.succesfulEnter();
                                } break;

                                case 2:
                                {
                                    if(model.getCountOfDep()!=0)
                                    {
                                    scan = new Scanner(System.in);
                                    model.printEmployee();
                                    view.setIndexOfEmployee();
                                    int select6 = scan.nextInt();
                                    while(select6 < 0 || select6 > model.getCount() - 1)
                                    {
                                        view.errorEnter();
                                        view.setIndexOfEmployee();
                                        select6 = scan.nextInt();
                                    }
                                    Department dep = new Department(name3, model.getEmployeeById(select6)) ;
                                    model.addDepartment(dep);
                                    Employee employee = new Employee(nameOfEmp,midNameOfEmp,lastnameOfEmp, dep, phoneOfEmp,payOfEmp);
                                    model.addEmployee(employee);
                                    view.succesfulEnter();
                                    }
                                    else {view.listEmpty();}
                                } break;

                                case 3:
                                {
                                    Department department = new Department(name3, null);
                                    Employee employee = new Employee(nameOfEmp,midNameOfEmp,lastnameOfEmp,department,phoneOfEmp,payOfEmp);

                                    department.setEmployee(employee);
                                    model.addDepartment(department);
                                    model.addEmployee(employee);
                                    view.succesfulEnter();

                                } break;

                                default: view.errorEnter();
                            }
                       }break;

                        case 2:
                        {
                          model.printDepartments();
                          view.setIndexOfDepartment();

                            int select6 = scan.nextInt();
                            while(select6<0||select6> model.getCountOfDep()-1)
                            {
                                view.errorEnter();
                               view.setIndexOfDepartment();
                                select6 = scan.nextInt();
                            }
                            Department dep = new Department(name, model.getEmployeeById(select6)) ;
                            model.addDepartment(dep);
                            Employee employee = new Employee(nameOfEmp,midNameOfEmp,lastnameOfEmp, dep, phoneOfEmp,payOfEmp);
                            model.addEmployee(employee);
                            view.succesfulEnter();
                        }break;

                        default: view.errorEnter();
                    }

                }
                break;

                case 3:
                {
                    int sel = -1;
                    int sel2 = -1;
                    scan = new Scanner(System.in);
                    view.case3();
                    view.setSelect();
                    sel = scan.nextInt();
                    switch(sel)
                    {
                        case 1:
                        {
                            if (model.getCount() == 0)
                            {
                                view.listIsEmpty();
                            }else{
                                view.setIndexOfEmployee();
                                scan = new Scanner(System.in);
                                sel2 = scan.nextInt();
                                if (sel2 >= 0 && sel2 < model.getCount()) {
                                    view.print(model.getEmployeeById(sel2));  
                                }else
                                {
                                view.noSuchEmployeeByID();
                                }
                            }
                        } break;

                        case 2:
                        {
                            if (model.getCount() == 0)
                            {
                                view.listIsEmpty();
                            }else{
                                scan = new Scanner(System.in);
                                view.setName();
                                String tmpName = scan.nextLine();
                                view.setLastName();
                                String tmpLastName = scan.nextLine();
                                int k = 0;
                                for (Employee tmp : model.getList()) {
                                    int i = 0;
                                    if (tmp.getLastName().equals(tmpLastName)) {
                                        if (tmp.getName().equals(tmpName)) {
                                            view.print(model.getEmployeeById(i));
                                            k++;
                                        }
                                    }
                                    i++;
                                }
                                if (k == 0) {
                                    view.noSuchEmployeeByName();
                                }
                            }
                        } break;
                    }
                }
                break;

                case 4:
                {
                    int sel = -1;
                    int sel2 = -1;
                    scan = new Scanner(System.in);
                    view.case4();
                    view.setSelect();
                    sel = scan.nextInt();
                    switch(sel)
                    {
                        case 1:
                        {
                            if (model.getCount() == 0)
                            {
                                view.listIsEmpty();
                            }else{
                                view.setIndexOfEmployee();
                                scan = new Scanner(System.in);
                                sel2 = scan.nextInt();
                                if (sel2 >= 0 && sel2 < model.getCount()) {
                                    model.deleteEmployeeById(sel2);
                                    view.EmployeeByIdDeleted(sel2);
                                }else
                                {
                                view.noSuchEmployeeByID();
                                }
                            }
                        } break;

                        case 2:
                        {
                            if (model.getCount() == 0)
                            {
                                view.listIsEmpty();
                            }else{
                                scan = new Scanner(System.in);
                                view.setName();
                                String tmpName = scan.nextLine();
                                view.setLastName();
                                String tmpLastName = scan.nextLine();
                                int k = 0;
                                for (int i = 0; i < model.getCount(); i++) {
                                    //int i = 0;
                                    if (model.getEmployeeById(i).getLastName().equals(tmpLastName)) {
                                        if (model.getEmployeeById(i).getName().equals(tmpName)) {
                                            model.deleteEmployeeById(i);
                                            view.EmployeeByNameDeleted(tmpName, tmpLastName);
                                            i--;
                                            k++;
                                        }
                                    }
                                }
                                if (k == 0) {
                                    view.noSuchEmployeeByName();
                                }
                            }
                        } break;
                    }
                }
                break;    
                    
                case 5:
                {



                } break;


                case 6:
                {
                   try{ model.serializeList(fileofEmployee); model.serializeDepartments(fileofDepartments);}
                   catch(IOException e)
                   {
                       view.errorofwrite();
                   }
                    view.succesfulEnter();

                }break;


                case 7:
                {

                    try{    model.loadList(fileofEmployee); model.loadDepartments(fileofDepartments);

                   }
                    catch(IOException e)
                    {
                        view.errorofread();
                    }
                    catch(ClassNotFoundException e)
                    {
                        view.errorofread();
                    }
                  view.succesfulRead();
                } break;
                case 8 :
                {
                    model.printEmployee();
                } break;

                case 9 :
                {
                    model.printDepartments();
                }  break;

                default:view.errorEnter();
            }
            view.menu();
            view.setSelect();
             scan = new Scanner(System.in);
            select= scan.nextInt();


        }



    }
}











